const organization = this.pageObj.getApiWidgetByAliasName('tree_organization');
const three = this.pageObj.getApiWidgetByAliasName('三维组件');
const search_input = this.pageObj.getApiWidgetByAliasName('search_input');
const text = $(search_input.__box).find('.input-filter').val();

const [ select ] = $event;
const value = select.value;
console.log(value, '全部用户/在线用户')

let fetchUrl = '/Communication/pubCommunicationAccount/getOrgListByName'
let option = {}

if (window.selCheckedNum == 3) {
  fetchUrl = '/Communication/pubCommunicationAccount/getAllAccountByEquipment'
  option = { method: 'POST', headers: {'Content-Type': 'application/json'},  body: "{}" }

  if (text) option.body = JSON.stringify({ communicationName: text })
} else {
  if (window.selCheckedNum == 1) {
    fetchUrl = '/Communication/pubCommunicationAccount/getOrgListByName'
  } else if (window.selCheckedNum == 2) {
    fetchUrl = '/Communication/pubCommunicationAccount/getGroupListByName'
  }

  if (text) fetchUrl += `?communicationName=${text}`
}

fetch(fetchUrl, option)
  .then(res => res.json())
  .catch(error => {
    console.error(error)
  })
  .then(res => {
    const { result, resultCode } = res;
    if (resultCode === '000000') {
      const artemis = three.widgetObj.artemis;
      const dataSource = result.map(breakDataSource);
      if (value === 'all') {
        organization.widgetObj._option.dataSource = artemis.resetUsersStatus(dataSource);
      } else {
        organization.widgetObj._option.dataSource = artemis.filterUsersOnline(dataSource);
      }

      organization.widgetObj.render();
      console.log(organization.widgetObj._option.dataSource)
    }
  });

function breakDataSource(item) {
  if(item.pointId || item.communicationName || item.personName) {
    item.id = item.personId;
    item.userId = item.account || item.communicationAccount;
    item.username = item.communicationName || item.personName;
    item.title = setTitle(item);
    item.treeId = item.userId;
    item.disabled = true
  } else if(item.orgId || item.orgName) {
    item.title = item.orgName;
    item.id = item.orgId;
    item.treeId = item.orgId;
  } else if (item.deptId || item.deptName) {
    item.title = item.deptName;
    item.id = item.deptId;
    item.treeId = item.deptId;
  } else if (item.distributionGroupId || item.distributionGroupName) {
    item.title = item.distributionGroupName;
    item.id = item.distributionGroupId;
    item.treeId = item.distributionGroupId;
  } else if (item.equipmentTypeName || item.equipmentTypeCode) {
    item.title = item.equipmentTypeName;
    item.id = item.equipmentTypeCode;
    item.treeId = item.equipmentTypeCode;
  }

  if (item.orgList) item.children = item.orgList.map(breakDataSource)
  if (item.groupList) item.children = item.groupList.map(breakDataSource)
  if (item.personList) item.children = item.personList.map(breakDataSource)
  if (item.accountData) item.children = item.accountData.map(breakDataSource)
  if (item.equipmentType) item.children = item.equipmentType.map(breakDataSource)

  return item
}

function setTitle ({ typeCode, communicationName, personName, username, userId } = {}) {
  let title = '';

  if (typeCode === 'dispatchDesk') title += '调度台';

  title += (communicationName || username || personName);

  if (userId) title += `(${userId})`;

  return title
}